package flux;

import sagex.api.MediaFileAPI;
import java.lang.Integer;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;



public class metadata
{
	public static String GetMediaTitle(Object MediaObject)
	{
		return MediaFileAPI.GetMediaFileMetadata(MediaObject, "MediaTitle");
	}

	public static String GetEpisodeTitle(Object MediaObject)
	{
		return MediaFileAPI.GetMediaFileMetadata(MediaObject, "EpisodeTitle");
	}
	
	public static long GetOriginalAirDate(Object MediaObject)
	/*returns the OriginalAiringDate as a long (in java date format) gathered from the metadta
	 * 0 if it does not exist or catches an error
	 * 
	 * @param MediaObject, a sage Airing, Show, or MediaFile
	 */
	{
		String s1 = MediaFileAPI.GetMediaFileMetadata(MediaObject, "OriginalAirDate");
		//System.out.println("OriginalAirDateString = '" + OrigAirDateString + "'");
		
		if (s1.length() == 0){
			//System.out.println("No Original Air Date");
			return 0;
		}
		//System.out.println("OriginalAirDateString = '" + OrigAirDateString + "'");
		try {
			DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			return sdf.parse(s1).getTime() + metadata.GetSeasonEpisodeNumber(MediaObject);
		}
		
		catch (ParseException e){
			System.out.println("Exception :"+e);
			return 0;
		}
	}
	
	public static String GetMediaType(Object MediaObject)
	{
		return MediaFileAPI.GetMediaFileMetadata(MediaObject, "MediaType");
	}
	
	public static boolean IsMediaTypeTV(Object MediaObject)
	{
		String Type = MediaFileAPI.GetMediaFileMetadata(MediaObject,"MediaType");
		if (Type.contains("TV")){
			return true;
		} else{
			return false;
		}
	}
	
	public static int GetDiscNumber(Object MediaObject)
	/*
	 * returns the Disc Number as an integer, 0 if it does not exist
	 * 
	 * @param MediaObject, a sage Airing, Show, or MediaFile Object
	 */
	{
		return Integer.parseInt(GetDiscNumberStr(MediaObject));
	}
	
	public static String GetDiscNumberStr(Object MediaObject)
	/*
	 * returns the Disc Number as a string, "0" if it does not exist
	 * 
	 * @param MediaObject, a sage Airing, Show, or MediaFile Object
	 */
	{
		String s1 = MediaFileAPI.GetMediaFileMetadata(MediaObject, "DiscNumber");
		
		if  (s1.length() == 0 ){
			return "0";
		} else{
			return s1;		
		}
	}
	
	public static String GetDiscNumberPad(Object MediaObject)
	/* returns the Season number as a formatted string with 0 Left padding, "00" if it does not exist
	 * 
  	 * @param MediaObject, a sage Airing, Show, or MediaFile Object
	 */
	{
		return String.format("%02d", GetDiscNumber(MediaObject));		
	}
	
	public static String GetMediaProviderDataID(Object MediaObject)
	{
		return MediaFileAPI.GetMediaFileMetadata(MediaObject, "MediaProviderDataID");
	}
	
	public static String GetUserRating(Object MediaObject)
	{
		return MediaFileAPI.GetMediaFileMetadata(MediaObject, "UserRating");
	}

	public static boolean IsDVD(Object MediaObject)
	/* returns true if Mediafile API calls IsDVD=true and IsBluRay=false
	 * 
	 * @param MediaObject, a Sage MediaFile
	 */
	{
		return (MediaFileAPI.IsDVD(MediaObject) && !MediaFileAPI.IsBluRay(MediaObject));
	}

	public static int GetSeasonNumber(Object MediaObject)
	/*
	 * returns the Season Number as an integer, 0 if it does not exist
	 * 
	 * @param MediaObject, a sage Airing, Show, or MediaFile Object
	 */
	{
		return Integer.parseInt(GetSeasonNumberStr(MediaObject));
	}
	

	public static String GetSeasonNumberStr(Object MediaObject)
	/* returns the Season Number as a String, "0" if it does not exist
	 * 
	 * @param MediaObject, a sage Airing, Show, or MediaFile Object
	 */
	{
		String s1 = MediaFileAPI.GetMediaFileMetadata(MediaObject, "SeasonNumber");
		
		if  (s1.length() == 0 ){
			return "0";
		} else{
			return s1;		
		}
	}
	
	public static String GetSeasonNumberPad(Object MediaObject)
	/* returns the Season number as a formatted string with 0 Left padding, "00" if it does not exist
	 * 
  	 * @param MediaObject, a sage Airing, Show, or MediaFile Object
	 */
	{
		return String.format("%02d", GetSeasonNumber(MediaObject));		
	}
	

	public static int GetEpisodeNumber(Object MediaObject)
	/* returns the Episode Number as an int, 0 if it does not exist
	 * 
	 * @param MediaObject, a sage Airing, Show, or MediaFile Object
	 */
	{
		return Integer.parseInt(GetEpisodeNumberStr(MediaObject));
	}

	public static String GetEpisodeNumberStr(Object MediaObject)
	/* returns the Episode Number as a String, "0" if it does not exist
	 * 
	 * @param MediaObject, a sage Airing, Show, or MediaFile Object
	 */
	{
		String s1 = MediaFileAPI.GetMediaFileMetadata(MediaObject, "EpisodeNumber");
		
		if  (s1.length() == 0 ){
			return "0";
		} else{
			return s1;		
		}
	}
	
	public static String GetEpisodeNumberPad(Object MediaObject)
	/* returns the Episode number as a formatted string with 0 Left padding, "00" if it does not exist
	 * 
  	 * @param MediaObject, a sage Airing, Show, or MediaFile Object
	 */
	{
		return String.format("%02d", GetEpisodeNumber(MediaObject));		
	}

	public static int GetSeasonEpisodeNumber(Object MediaObject)
	/*
	 * returns the SeasonEpisode Number as an integer
	 * or 0 if Season Number does not exist
	 * 101 (s01e01)
	 * 201 (s02e01)
	 * 1010 (s10e10)
	 * 
	 * @param MediaObject, a sage Airing, Show, or MediaFile Object
	 */
	{
		int sn = metadata.GetSeasonNumber(MediaObject);
		int en = metadata.GetEpisodeNumber(MediaObject);
		
		if (sn == 0){
			return 0;
		} else{	
			return sn*100 + en;
		}
	}
}
